package com.v.air.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.v.air.app.R;
import com.v.air.app.bean.PM25Bean;
import com.v.air.app.interfaces.StationClickListener;

@SuppressLint("ResourceAsColor")
public class StationListAdapter extends BaseAdapter {
	private List<PM25Bean> data = new ArrayList<PM25Bean>();
	private LayoutInflater inflater;
	private StationClickListener listener;
	private int lastPosition = -1;

	public StationListAdapter(Context context,
			StationClickListener deviceClickListener) {
		this.inflater = LayoutInflater.from(context);
		this.listener = deviceClickListener;
	}

	public int getLastPosition() {
		return lastPosition;
	}

	public void setLastPosition(int lastPosition) {
		this.lastPosition = lastPosition;
	}

	public List<PM25Bean> getData() {
		return data;
	}

	public void setData(List<PM25Bean> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int id) {
		// TODO Auto-generated method stub
		return id;
	}

	public class ViewHolder {
		private RelativeLayout layout;
		private ImageView imageView;
		private TextView nameTextView;
		private TextView contentTextView;
		private TextView stateTextView;
	}

	public View getView(final int position, View contentView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = inflater.inflate(R.layout.city_item, null);
			holder.layout = (RelativeLayout) contentView
					.findViewById(R.id.layout);
			holder.imageView = (ImageView) contentView.findViewById(R.id.image);
			holder.nameTextView = (TextView) contentView
					.findViewById(R.id.name);
			holder.stateTextView = (TextView) contentView
					.findViewById(R.id.state);
			holder.contentTextView = (TextView) contentView
					.findViewById(R.id.content);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		final PM25Bean bean = data.get(position);
		holder.nameTextView.setText(bean.getPosition_name());
		holder.nameTextView.setTag(bean);
		holder.contentTextView.setText(bean.getPm2_5() + "");
		holder.stateTextView.setText(bean.getQuality());
		holder.imageView.setImageResource(R.drawable.c5_icon);
		if(position==lastPosition){
			holder.layout.setBackgroundColor(R.color.color_left_list_select);
		}else {
			holder.layout.setBackgroundColor(R.color.color_left_list_nomal);	
		}

		holder.layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.onClick(bean, position);
			}
		});
		return contentView;
	}
}
